package com.vadmin.vadminjava.service;

import com.alibaba.fastjson.JSONObject;
import com.vadmin.vadminjava.common.ResultSetToJavaBean;
import com.vadmin.vadminjava.db.DBUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;


/* ***********************************************************
 *
 * 微信管理后台的用户相关实现
 *
 * **********************************************************/
@Component
public class AdminWxUserService extends CommonExtends {

	org.slf4j.Logger logger = LoggerFactory.getLogger(AdminWxUserService.class.getName());



	/**
	 * 企业微信员工列表，注意，这是员工列表
	 * @param params
	 * @return
	 */
	public JSONObject wxWorkUserList(JSONObject params) {
		return commonDataList(params,"wxwork_user");
	}

	/**
	 * 企业微信外部用户列表
	 * @param params
	 * @return
	 */
	public JSONObject wxWorkExternalUserList(JSONObject params) {
		//return commonDataList(params,"wxwork_external_user");
		JSONObject map = new JSONObject();
		map.put("resultFlag", false);
		DBUtil dbUtil = new DBUtil();
		//sql处理
		Integer page      = params.getInteger("page");
		Integer pageSize  = params.getInteger("pageSize");
		String startTime = params.getString("startTime");
		String endTime   = params.getString("endTime");

		//参数处理
		//if(StringUtils.isEmpty(page)) page = 1;
		//if(StringUtils.isEmpty(pageSize))pageSize = 20;
		if(page==null) page = 1;
		if(pageSize==null)pageSize = 20;

		//sql处理
		String countSql = "SELECT COUNT(0) as count FROM wxwork_external_user WHERE 1=1 ";
		String sql = "select weu.*,wur.open_id from wxwork_external_user as weu" +
				" left join wx_user_relation wur" +
				" on wur.external_userid = weu.external_userid" +
				" where 1=1 ";

		sql += " order by weu.id desc ";
		sql += " limit " + (page - 1) * pageSize + "," + pageSize;

		//执行
		try {
			ArrayList<JSONObject> objects = ResultSetToJavaBean.copyJson(dbUtil.executeQuery(sql));
			int                   count   = ResultSetToJavaBean.copyJsonSingleQuery(dbUtil,countSql).getInteger("count");


			map.put("count",count);
			map.put("data", objects);
			map.put("resultFlag", true);
			map.put("resultMsg", "操作成功");
		} catch (Exception e) {
			map.put("resultMsg", "操作失败");
		} finally {
			dbUtil.closeNoErr();
		}
		return map;
	}


	/**
	 * 从数据库获取用户（员工）信息
	 * @param params
	 * @return
	 */
	public JSONObject userData(JSONObject params){
		String userId = params.getString("userId");
		String sql = "select * from wxwork_user where user_id = '"+userId+"'";
		return commonDataSingle(sql);
	}

	/**
	 * 从数据库获取外部客户信息
	 * @param params
	 * @return
	 */
	public JSONObject externalUserData(JSONObject params){
		String externalUserid = params.getString("externalUserid");
		String sql = "select * from wxwork_external_user where external_userid = '"+externalUserid+"'";
		return commonDataSingle(sql);
	}

	/**
	 * 员工的所属客户列表
	 * @param params
	 * @return
	 */
	public JSONObject wxWorkExternalUserListOfUser(JSONObject params) {

		String condition = "";
		String table = "wxwork_external_user";

		//条件处理
		String userId = params.getString("userId");//员工
		if(StringUtils.isEmpty(userId)) userId = "-";
		if(StringUtils.isNotEmpty(userId) ) {
			condition += " AND userid = '" + userId + "' ";
		}
		return commonDataListFlexible(table,condition,params);
	}
}
